package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"z-blog/e"
	"z-blog/models"
)

func GetTagAll(c *gin.Context) {
	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	code := e.SUCCESS

	data["list"] = models.GetTag(maps)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

func AddTag(c *gin.Context) {
	data := make(map[string]interface{})
	code := e.SUCCESS

	data["Name"] = c.Query("name")
	data["CreatedBy"] = c.Query("created_by")
	data["Time"] = c.Query("time")

	if models.InsertTag(data) != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": nil,
	})
}

func DelTag(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	code := e.SUCCESS

	if models.DelTag(id) != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": nil,
	})
}
